﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDT.Task.Plan;
using System.Threading;

namespace ZDT.Task.Job
{
    public enum TashState
    {
        None,
        Initing,
        Inited,
        Starting,
        Started,
        Stoping,
        Stoped
    }
    public class TaskItem
    {
        public TaskItem(ISchedule schedule)
        {
            Schedule = schedule;
        }
        private Timer timer = null;
        private DateTime m_lastExecuteTime = DateTime.Now;
        private DateTime m_nextExecuteTime = DateTime.Now;
        private TimerCallback m_execTask;
        private string m_name;
        private object m_param;
        protected virtual void Init()
        {

        }
        public void Start()
        {
            if (!IsRunning)
            {
                IsRunning = true;
                State = TashState.Initing;
                this.Init();
                State = TashState.Inited;
                State = TashState.Starting;
                timer = new Timer(m_execTask, null, Schedule.DueTime, Schedule.Period);
                State = TashState.Started;
            }
        }
        public void Stop()
        {
            if (IsRunning)
            {
                State = TashState.Stoping;
                timer.Change(Timeout.Infinite, Timeout.Infinite);
                State = TashState.Stoped;
                IsRunning = false;
            }
        }
        public void execTask(object state)
        {

        }
        public virtual void Execute(object param)
        {
            //你需要重载该函数,但是需要在你的新函数中调用base.Execute();
            m_lastExecuteTime = DateTime.Now;
            if (Schedule.Period == Timeout.Infinite)
            {
                m_nextExecuteTime = DateTime.MaxValue; //下次运行的时间不存在
            }
            else
            {
                TimeSpan period = new TimeSpan(Schedule.Period * 1000);
                m_nextExecuteTime = m_lastExecuteTime + period;
            }  
        }
        /// <summary>           
        /// 任务下执行时间           
        /// </summary>           
        public DateTime NextExecuteTime
        {             
            get              
            {                 
                return m_nextExecuteTime;
            }          
        }         
        /**/          
        /// <summary>            
        /// 执行任务的计划           
        /// </summary>          
        public ISchedule Shedule
        {              
            get             
            {                  
                return m_schedule;
            }         
        }          
        private ISchedule m_schedule;
        /**/          
        /// <summary>          
        /// 任务内容           
        /// </summary>           
        public TimerCallback Job 
        {             
             get             
             {              
                 return m_execTask;     
             }             set    
             {                  
                 m_execTask = value;     
             }       
        }
        /**/
        /// <summary>          
        /// 任务参数          
        /// </summary>          
        public object JobParam
        {
            set
            {
                m_param = value;
            }
        }
        /**/
        /// <summary>           
        /// 任务名称           
        /// </summary>          
        public string Name
        {
            get
            {
                return m_name;
            }
            set
            {
                m_name = value;
            }
        }        
        /**/
        /// <summary>          
        /// 任务描述          
        /// /// </summary>           
        public string Description
        {
            get
            {
                return m_description;
            }
            set
            {
                m_description = value;
            }
        }          private string m_description;
        /**/
        /// <summary>           
        /// 该任务最后一次执行的时间          
        /// </summary>           
        public DateTime LastExecuteTime
        {
            get
            {
                return m_lastExecuteTime;
            }
        }
        public ISchedule Schedule { get; private set; }
        public bool IsRunning { get; private set; }
        public bool IsFinished { get; protected set; }
        public TashState State { get; protected set; }
    }
}
